import WebUploader from 'webuploader/dist/webuploader.js';
import PaBaseModel from '../../core/model/PaBaseModel';

function workerInfoDetailNewCtrl($scope, $http, ngDialog, paFetchService) {
  'ngInject';

  let _this = this;

  this.editableUserInfo = {
    fileIds: [],
    fileBeanList: [],
    isspecialMan: '0', // 是否是特种人员,默认给否
  };

  this.isSpecialMandatory = false; // 特种人员显示证书编号等为必填

  // 是否是特种人员
  this.isSpecial = () => {
    if(this.editableUserInfo.isspecialMan == '1'){
      this.isSpecialMandatory = true; // 特种人员显示证书编号等为必填
    }else {
      this.isSpecialMandatory = false;
    }
  }

  // 保存基本信息
  this.save = (param) => {
    // 修改表单是否提交过状态
    param.$submitted = true;

    // 表单校验
    if (param.$invalid) {
      return false;
    }

    // 获得上传附件的id列表
    this.editableUserInfo.fileIds = this.editableUserInfo.fileBeanList.map((item) =>{
      return item.id;
    });
    $http.post(`${SERVER_CONTEXT}/api/safesite/workerInfo/saveRecord`,this.editableUserInfo).
      then((resp) => {
        if(resp.data.success){
          layer.msg('添加成功');
          $state.go('workerInfoDetail',{id:resp.data.message});
        } else {
          layer.alert(resp.data.message);
        }
    });
  };

  // 获得施工队list             
  paFetchService
                .getTeamWithOrgList()
                .then((resp) => {
                  this.teamWithOrgList = resp.data;
                });

  // 重置基本信息
  this.reset = () => {
    this.editableUserInfo = {}; 
  };

  // 获取岗位类别
  this.getJobCategory = () => {
    $http.get(`${SERVER_CONTEXT}/api/organization/queryDictionaryListByType/岗位类别`).
      then((resp) => {
        if(resp.statusText == 'OK'){
          this.categoryList = resp.data;
        }
    });
  }

  // 获取作业类别
  this.getWorkCategory = () => {
    $http.get(`${SERVER_CONTEXT}/api/organization/queryDictionaryListByType/作业类别`).
      then((resp) => {
        if(resp.statusText == 'OK'){
          this.workList = resp.data;
        }
    });
  }

  this.initPage = () => {
    // 获取岗位类别
    this.getJobCategory();

    // 获取作业类别
    this.getWorkCategory();
  }

  // 初始化页面
  this.initPage();

}

export default workerInfoDetailNewCtrl;